package com.pboc.web;

import com.pboc.base.BaseException;
import com.pboc.base.PageModel;
import com.pboc.entity.SysPerson;
import com.pboc.service.SysPersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/person")
public class SysPersonController {
    @Autowired
    private SysPersonService sysPersonService;
    @RequestMapping("/view")
    public String personView(){
        return "/person/person_view";
    }

    @RequestMapping("list")
    @ResponseBody
    public PageModel getList(PageModel<SysPerson> pageModel,SysPerson sysPerson,Integer rows,Integer page){
        pageModel.setPageSize(rows);
        pageModel.setPageNumber(page);
        pageModel.setQueryObj(sysPerson);
        sysPersonService.getList(pageModel);
        return pageModel;
    }

    /**
     * 根据部门获取人员信息
     * @param deptId 部门id
     * @return
     */
    @RequestMapping("/getPersonByDept")
    @ResponseBody
    public List<SysPerson> getPersonByDept(int deptId){
        return sysPersonService.getPersonByDept(deptId);
    }
    @RequestMapping("info")
    public String toPersonInfoView(Integer userId, Model model) {
        if(userId!=null){
            model.addAttribute("person", sysPersonService.getById(userId));
        }
        return "/person/person_info";
    }
    @RequestMapping("/del")
    @ResponseBody
    public Map del(Integer id){
        Map map = new HashMap();
        try{
            sysPersonService.deletePerson(id);
            map.put("success", true);
        }catch (Exception e){
            e.printStackTrace();
            map.put("success", false);
            map.put("mes", "系统内部错误");
        }
        return map;
    }
    @RequestMapping("/addperson")
    @ResponseBody
    public Map addperson(SysPerson sysPerson) {
        Map map = new HashMap();
        try{
            sysPersonService.insertPerson(sysPerson);
            map.put("success", true);
        }catch (BaseException e){
            map.put("success", false);
            map.put("mes", e.getMessage());
        }
        catch (Exception e){
            e.printStackTrace();
            map.put("success", false);
            map.put("mes", "系统内部错误");
        }
       return map;
    }
    @RequestMapping("/editPerson")
    @ResponseBody
    public Map editPerson(SysPerson sysPerson) {
        Map map = new HashMap();
        try{
            sysPersonService.updatePerson(sysPerson);
            map.put("success", true);
        }catch (BaseException e){
            map.put("success", false);
            map.put("mes", e.getMessage());
        }
        catch (Exception e){
            e.printStackTrace();
            map.put("success", false);
            map.put("mes", "系统内部错误");
        }
        return map;
    }
    @RequestMapping("carPayPrint")
    public String carPayPrint(Model model){
        model.addAttribute("month", getMonthStr(new Date(), -1));
        model.addAttribute("carPayList", sysPersonService.getCarPayPrintList());
        return "/wage/carPayPrint";
    }
    /**
     * 获取时间的年月字符串。
     *
     * @param date     时间对象
     * @param addMonth 月份计算加减值
     * @return 年月字符串 yyyy-MM
     * @author licheng
     * @since V1.0.0
     */
    public String getMonthStr(Date date, int addMonth) {
        //格式化为年月
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Calendar curr = Calendar.getInstance();
        curr.setTime(date);
        curr.add(Calendar.MONTH, addMonth);
        return sdf.format(curr.getTime());
    }
}